<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="mainor_form batch-wrap">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
            id="form"
          >
            <el-form-item label="" prop="mode">
              <el-select
                v-model.trim="form.mode"
                placeholder="采购方式"
                class="wid100 methods"
                @change="changeMode"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(modeList, index) in list.modeList"
                  :key="index"
                  :label="modeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
              <el-select
                v-model.trim="form.buyer_id"
                placeholder="采购员"
                class="wid220 marginLeft5s"
                v-show="form.mode == 1"
                filterable
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(buyer, index) in list.buyer"
                  :key="index"
                  :label="buyer"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
              <el-select
                v-model.trim="form.supplier_id"
                placeholder="供应商"
                class="wid220 marginLeft5s"
                v-show="form.mode == 2"
                filterable
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(supplier, index) in list.supplier"
                  :key="index"
                  :label="supplier"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
              <el-select
                v-model.trim="form_mode_all"
                placeholder="所有"
                class="wid220 marginLeft5s"
                v-show="!form.mode"
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="账期：" prop="payment_days">
              <el-date-picker
                v-model="form.payment_days"
                type="months"
                placeholder="选择多月"
                value-format="yyyy-MM"
                prefix-icon="false"
                @change="getArticleList"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="账单日期：" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
                @change="getArticleList"
              />
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getArticleList" :loading="listLoading" :showBtn="false">
              <el-button @click="export_de">导出</el-button>
            </MainFormBtn>
          </el-form>
        </div>
        <div class="main_table mg-t-4">
          <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list.list"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          @selection-change="handleSelectionChange"
          :header-cell-class-name="this.$verticaline"
          :height="borderHeight"
          :header-cell-style="tableHeightStyle"
          @header-dragend="changeTableWidth"
        >
          <el-table-column type="selection" align="center" />
          <el-table-column label="采购方式" 
          prop="mode"
          :width="tableWidth[hash+'mode']">
            <template slot-scope="scope">{{
              list.modeList[scope.row.mode]
            }}</template>
          </el-table-column>
          <el-table-column label="采购员/供应商"
          prop="buyer_id"
          :width="tableWidth[hash+'buyer_id']">
            <template slot-scope="scope">{{
              scope.row.mode == 1
                ? list.buyer[scope.row.buyer_id]
                : list.supplier[scope.row.supplier_id]
            }}</template>
          </el-table-column>
          <el-table-column label="应付金额" prop="money" 
          :width="tableWidth[hash+'money']"/>
          <el-table-column label="已付金额" prop="paid" 
          :width="tableWidth[hash+'paid']"/>
          <el-table-column label="抹零金额" prop="zero_money" 
          :width="tableWidth[hash+'zero_money']"/>
          <el-table-column label="未付金额" prop="unpaid" 
          :width="tableWidth[hash+'unpaid']"/>
          <el-table-column label="联系人" prop="name" 
          :width="tableWidth[hash+'name']"/>
          <el-table-column label="手机号" prop="mobile" 
          :width="tableWidth[hash+'mobile']"/>
          <el-table-column
            label="管理操作"
            align="center"
            fixed="right"
            prop="operate"
            :width="tableWidth[hash+'operate'] || 140"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <el-link type="success" @click="handleJump(scope.row)"
                >结算</el-link
              >
              <!-- <router-link to="/financial/recharge">
                 
                </router-link> -->
            </template>
          </el-table-column>
        </el-table>
        </div>
        <div class="main_num boxdq mg-t-10">
          <div class="main_num_l box">
            <p>
              应付总额：
              <em>{{ list.money }}</em>
            </p>
            <p>
              已付总额：
              <em>{{ list.paid }}</em>
            </p>
            <p>
              抹零金额：
              <em>{{ list.zero_money_all }}</em>
            </p>
            <p>
              未付总额：
              <em>{{ list.unpaid }}</em>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  account_generalLedger,
  export_account_info,
} from "@/api/financial/financialPre";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { getWeekStartAndEnd, parseTime, exportMethod } from "@/utils/index";
export default {
  mixins: [mixins],
  components: {},
  data() {
    return {
      // 列表数据
      exportMethod,
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        start_time: "",
        end_time: "",
        mode: null,
        buyer_id: null,
        supplier_id: null,
        payment_days: [parseTime(new Date(), "{y}-{m}")],
      },
      form_mode_all: "",
      createTime: [],
      pickerOptions: dateOption,
      selectArr: [],
    };
  },
  created() {
    // 列表
    this.getNumerical(135);
    this.account_generalLedger();
  },
  methods: {
    startGetting() {
      this.getArticleList();
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item, index) => {
        this.selectArr.push(item.index + 1);
      });
    },
    // 导出明细
    export_de() {
      let ids = "";
      if (this.selectArr.length > 0) {
        ids = this.selectArr.join("-");
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { ids: ids },
        this.list_form()
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_account_info(data)
        .then((res) => {
          this.exportMethod(res, "采购总账", notify);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 搜索
    getArticleList() {
      this.account_generalLedger();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.form.buyer_id = "";
      this.form.supplier_id = "";
      this.getArticleList();
    },
    // 采购方式切换
    changeMode() {
      this.form.buyer_id = null;
      this.form.supplier_id = null;
      this.form_mode_all = "";
    },
    // 列表
    account_generalLedger() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      account_generalLedger(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.forEach((e, index) => {
            e.index = index;
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.payment_days = this.form.payment_days
        ? this.form.payment_days.toString()
        : "";
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";

      return form;
    },
    // 结算
    handleJump(row) {
      let mode = row.mode;
      let buyer_id = row.buyer_id;
      let supplier_id = row.supplier_id;
      let payment_days = this.form.payment_days;
      this.$router.push({
        name: "financial_recharge",
        params: {
          mode: mode,
          payment_days: payment_days,
          buyer_id: buyer_id,
          supplier_id: supplier_id,
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 220px);
  }
}

.wid100 {
  width: 100px;
}

.marginLeft5s {
  margin-left: -5px;
}
/deep/.methods .el-input__inner {
  border: none;
}
/deep/ .el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
